package com.gs.disk.controller.common;

import cn.hutool.core.util.ObjectUtil;
import com.gs.entity.OssFile;
import com.gs.entity.R;
import com.gs.common.util.DateTimeUtil;
import com.gs.common.util.FileUtils;
import com.gs.disk.entity.OssEntity;
import com.gs.disk.service.common.MinioService;
import com.gs.disk.service.common.OssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @title: 孙杨申
 * @date 2022/2/8 10:58
 */

@RestController
@RequestMapping("disk/common/oss")
@Api(tags = "系统OSS")
public class OssController {

    @Resource
    private MinioService minioService;

    @Resource
    private OssService ossService;



    /**
     * 上传文件
     * @param file 文件
     * @param bucketName bucket名字
     * @return
     */
    @PostMapping("upload")
    @ApiOperation("上传文件")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "file", value = "文件", paramType = "form", dataType = "MultipartFile"),
    })
    public R upload(@RequestParam("file") MultipartFile file, @RequestParam String bucketName, @RequestParam String path) {
        String originalFilename = file.getOriginalFilename();
        String ossFileName = FileUtils.getFileNameForDateTime() + "." + FileUtils.getFileSuffix(originalFilename);

        if(ObjectUtil.isEmpty(path)) {
            path = DateTimeUtil.currentTimeUTf8();
        }
        // 上传文件的path必须以“/”结尾
        if(path != null && !path.endsWith("/")) {
            path += "/";
        }

        minioService.upload(file, bucketName, path, ossFileName);

        // 创建oss文件对象
        OssEntity ossEntity = new OssEntity(null, bucketName, path,ossFileName, originalFilename , 0,1, LocalDateTime.now());
        ossService.insert(ossEntity);
        return new R().ok(ossEntity.getId());
    }

    /**
     * 创建文件夹
     * @param path 要创建的路径
     * @return
     */
    @GetMapping("mkdir/{bucket}/{path}")
    @ApiOperation("创建文件夹")
    public R mkdir(@PathVariable String bucket, @PathVariable String path) {
        OssEntity ossEntity = new OssEntity();
        ossEntity.setBucket(bucket);
        ossEntity.setPath(path);
        ossEntity.setDirectory(1);
        ossEntity.setDataStatus(1);
        ossEntity.setUpdateTime(LocalDateTime.now());
        ossService.insert(ossEntity);
        return new R();
    }


    /**
     * 下载文件/查看图片
     * @param id 本地文件id
     * @param res 请求头
     * @return
     */
    @GetMapping("download/{id}")
    @ApiOperation("下载文件")
    public void download(@PathVariable Long id, HttpServletResponse res) {
        OssEntity ossEntity = ossService.get(id);
        minioService.download(ossEntity.getBucket(),
                (ossEntity.getPath() == null ? "" : ossEntity.getPath()) + ossEntity.getOssFileName(),
                res);
    }

    /**
     * 删除文件
     * @param id 文件id
     * @return
     */
    @GetMapping("delete/{id}")
    @ApiOperation("删除文件")
    public R delete(@PathVariable Long id) {
        OssEntity ossEntity = ossService.get(id);
        String bucketName = ossEntity.getBucket();
        String path = ossEntity.getPath();
        String ossFileName = ossEntity.getOssFileName();
        minioService.delete(bucketName, path + ossFileName);
        return new R();
    }

    /**
     * 获取所有文件列表
     * @param bucketName bucketName
     * @param path 文件路径
     * @return
     */
    @GetMapping("list")
    @ApiOperation("文件列表查看")
    public R listAllFiles(String bucketName, String path) {
        List<OssFile> ossFiles = minioService.listAllFiles(bucketName, path);
        return new R().ok(ossFiles);
    }


}
